<?php
class MemberMoneylogModel extends Model{
	private $table1 = 'member_moneylog';
	private $table2 = 'member_withdraw';
	private $table3 = 'money_type';
	private $table4 = 'membership';
	//视图表
	private $table5 = 'withdrawdetail';
	
	/**
	 * 获取会员提现记录
	 * @param array $where string $order string $limit string $field
	 * @return array 
	 * 
	 */
	public function GetWithdraw($where,$order,$limit){
		 //return M($this->table2)->table($this->table2." mw")->join($this->table4." m ON mw.uid=m.uid")->where($where)->field($field)->order($order)->limit($limit)->select();
		return M($this->table5)->where($where)->order($order)->field($field)->limit($limit)->select();
		// echo M($this->table2)->getLastSql();die;
	}
	
	//获取提现次数
	public function getWcount($where){
		return M($this->table5)->where($where)->count();
	}
	
	/**
	 * 订单买家确定收货，评价后，卖家的金额有冻结转为活动金额
	 * @param $uid 卖家会员id $affect_money 变动金额 $order_id 订单id
	 */
	public function changeFtoA($uid,$affect_money,$order_id){
		$this->startTrans();
		$dflag = false;
		$change_moneylog = $this->table($this->table1)->where(array('uid'=>$uid))->field('active_money,freeze_money')->order('member_moneylog_id desc')->find();
		$data['uid'] = $uid;
		$data['money_type'] = 8;
		$data['affect_money'] = $affect_money;
		$data['active_money'] = $change_moneylog['active_money'] + $affect_money;
		$data['freeze_money'] = $change_moneylog['freeze_money'] - $affect_money;
		$data['freeze_flag'] = 1;
		$data['moneylog_affect_date'] = date('Y-m-d H:i:s', time());
		$data['order_id'] = $order_id;
		$data['u_ip'] = getip();
		$data['comment'] = '买家确认收货，评价结束';
		$result = $this->table($this->table1)->add($data);
		if($result){
			$result = M('order')->where(array('order_id'=>$order_id))->setField('order_status_id', '6');//修改订单状态为已评价
			if($result){
				$this->commit();
				$dflag = true;
			}
		}
		if(!$dflag){
			$this->rollback();
		}
		
		return $dflag;
		
	}
	/**
	 * 处理资金变动,前端买家取消订单，处理
	 * @param string array $data 订单的数据
	 * @return flag变动状态
	 */
	public function Moneychange($data){
		//var_dump($data);die;
		//$model = new Model();
		$this->startTrans();
		$dflag = false;
		//获取最新资金记录
		$change_moneylog = $this->table($this->table1)->where(array('uid'=>$data['publish_uid']))->field('active_money,freeze_money')->order('member_moneylog_id desc')->find();
		//扣除卖家金额
		$cdata['active_money'] =$change_moneylog['active_money'] ;//+ $data['pay_price'];
		$cdata['freeze_money'] = $change_moneylog['freeze_money'] - $data['pay_price'];
		$cdata['affect_money'] = $data['pay_price'];
		$cdata['uid'] =  $data['publish_uid'];
		$cdata['money_type'] = 5;
		$cdata['moneylog_affect_date'] = date('Y-m-d H:i:s',time());
		$cdata['order_id'] = $data['order_id'];
		$cdata['u_ip'] = getip();
		$result = $this->table($this->table1)->add($cdata);
		if($result){//卖家扣款成功，奖金额转给买家账户
			$change_moneylog = $this->table($this->table1)->where(array('uid'=>$data['uid']))->field('active_money,freeze_money')->order('member_moneylog_id desc')->find();
			$bdata['active_money'] =$change_moneylog['active_money'] + $data['pay_price'];
			$bdata['freeze_money'] = $change_moneylog['freeze_money'];
			$bdata['affect_money'] = $data['pay_price'];
			$bdata['uid'] =  $data['uid'];
			$bdata['money_type'] = 6;
			$bdata['moneylog_affect_date'] = date('Y-m-d H:i:s',time());
			$bdata['order_id'] = $data['order_id'];
			$bdata['u_ip'] = getip();
			$result = $this->table($this->table1)->add($bdata);
			if($result){
				$this->commit();
				$dflag = true;
			}
		}
		//echo $dflag;die;
		if(!$dflag){
			$this->rollback();
		}
		
		return $dflag;
	}
	
	/**
	 * 处理资金变动，后台管理人员取消订单处理
	 * @param string array $data 订单的数据
	 * @return flag变动状态
	 */
	public function MoneychangeS($data){
		$model = new Model();
		$model->startTrans();
		$dflag = false;
		//判断卖家交易金额是否已经解冻
		$moneylog = M('member_moneylog')->where(array('uid'=>$data['publish_uid'],'order_id'=>$data['order_id']))->count();
		//扣除卖家交易金额
		if($moneylog==2){//订单金额已经转为用户活动金额
			//获取最新资金记录
			$change_moneylog = $model->table($this->table1)->where(array('uid'=>$data['publish_uid']))->field('active_money,freeze_money')->order('member_moneylog_id desc')->find();
			$cdata['active_money'] =$change_moneylog['active_money']-$data['pay_price'];
			$cdata['freeze_money'] = $change_moneylog['freeze_money'];
				
		}elseif($moneylog==1){//订单金额还未冻结状态
			//获取最新资金记录
			$change_moneylog = $model->table($this->table1)->where(array('uid'=>$data['publish_uid']))->field('active_money,freeze_money')->order('member_moneylog_id desc')->find();
			$cdata['active_money'] =$change_moneylog['active_money'];
			$cdata['freeze_money'] = $change_moneylog['freeze_money']-$data['pay_price'];			
		}
		
		$cdata['affect_money'] = $data['pay_price'];
		$cdata['uid'] =  $data['publish_uid'];
		$cdata['money_type'] = 5;
		$cdata['moneylog_affect_date'] = date('Y-m-d H:i:s',time());
		$cdata['order_id'] = $data['order_id'];
		$cdata['u_ip'] = getip();
		$result = $model->table($this->table1)->add($cdata);
		if($result){//卖家扣款成功，奖金额转给买家账户
			$change_moneylog = $model->table($this->table1)->where(array('uid'=>$data['uid']))->field('active_money,freeze_money')->order('member_moneylog_id desc')->find();
			$bdata['active_money'] =$change_moneylog['active_money'] + $data['pay_price'];
			$bdata['freeze_money'] = $change_moneylog['freeze_money'];
			$bdata['affect_money'] = $data['pay_price'];
			$bdata['uid'] =  $data['uid'];
			$bdata['money_type'] = 6;
			$bdata['moneylog_affect_date'] = date('Y-m-d H:i:s',time());
			$bdata['order_id'] = $data['order_id'];
			$bdata['u_ip'] = getip();
			$result = $model->table($this->table1)->add($bdata);
			if($result){
				$model->commit();
				$dflag = true;
			}
		}
		if(!$dflag){
			$model->rollback();
		}
	
		return $dflag;
	}
}